package com.project_doctor.service.impl;

import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project_doctor.common.UserStatus;
import com.project_doctor.handler.Assert;
import com.project_doctor.mapper.PersonalProfileMapper;
import com.project_doctor.pojo.PersonalProfile;
import com.project_doctor.pojo.dto.PersonalProfileDto;
import com.project_doctor.pojo.enums.BusinessCode;
import com.project_doctor.service.PersonalProfileService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

/**
* @author 龙龙与贝儿
* @description 针对表【t_personal_profile】的数据库操作Service实现
* @createDate 2025-05-29 19:33:32
*/
@Service
public class PersonalProfileServiceImpl extends ServiceImpl<PersonalProfileMapper, PersonalProfile>
    implements PersonalProfileService{
    @Autowired
    PersonalProfileMapper personalProfileMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public ResponseEntity doLogin(PersonalProfileDto personalProfileDto) {
        //获取验证码的key
        String key = personalProfileDto.getKey();
        //得到Redis中验证码
        String code = redisTemplate.opsForValue().get(key).toString();
        //判断验证码是否过期
        Assert.error(code==null, BusinessCode.INVALID_VERIFICATION);
        //        if (code == null) {
        //            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("验证码已过期");
        //        }

        //判断验证码是否正确
        Assert.error(!code.equalsIgnoreCase(personalProfileDto.getCode()), BusinessCode.VERIFICATION_ERROR);
        //        if (!code.equalsIgnoreCase(userDto.getCode())) {
        //            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("验证码错误");
        //        }
        //删除Redis中的验证码,每次验证后验证码会被删除；
        redisTemplate.delete(key);

        LambdaQueryWrapper<PersonalProfile> wrapper = Wrappers.lambdaQuery(PersonalProfile.class).eq(PersonalProfile::getPhone, personalProfileDto.getPhone());
        PersonalProfile user = personalProfileMapper.selectOne(wrapper);

        Assert.error(user==null, BusinessCode.USER_NOT_EXIST);
        //        if (user==null) {
        //            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("用户不存在！");
        //        }
        BCrypt encoder=new BCrypt();
        boolean match = encoder.checkpw(personalProfileDto.getPassword(), user.getPassword());
        Assert.error(!match, BusinessCode.PASSWORD_ERROR);
        //        if (!match) {
        //            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("密码不正确！");
        //        }

        Assert.error(user.getState()!= UserStatus.ACTIVE.getKey(),BusinessCode.USER_FROZEN_ERROR);
        //        if (user.getState()!= UserStatus.ACTIVE.getKey()){
        //            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("状态锁定，无法登陆！");
        //
        //        }
        return ResponseEntity.ok(user);
    }


    @Override
    public PersonalProfile findUserByMobile(String mobile) {
        LambdaQueryWrapper<PersonalProfile> wrapper = Wrappers.lambdaQuery(PersonalProfile.class).eq(PersonalProfile::getPhone, mobile);
        return personalProfileMapper.selectOne(wrapper);
    }



    @Override
    public boolean isMobileExist(String mobile) {
        LambdaQueryWrapper<PersonalProfile> wrapper = Wrappers.lambdaQuery(PersonalProfile.class).eq(PersonalProfile::getPhone, mobile);
        return personalProfileMapper.selectOne(wrapper)!= null;
    }

    @Override
    public PersonalProfile register(PersonalProfile user) {
        //对密码进行加密
        user.setPassword(BCrypt.hashpw(user.getPassword()));
        //设置用户状态
        user.setState(UserStatus.ACTIVE.getKey());
        //写入数据库，会自动更新user的id值
        personalProfileMapper.insert(user);
        return user;
    }

    @Override
    public boolean isUsernameExist(String username) {
        LambdaQueryWrapper<PersonalProfile> wrapper = Wrappers.lambdaQuery(PersonalProfile.class).eq(PersonalProfile::getUsername, username);
        return personalProfileMapper.selectOne(wrapper)!= null;
    }



    @Override
    public Integer editUser(PersonalProfile user) {
        Integer i = personalProfileMapper.updateById(user);
//        PersonalProfile user1=new PersonalProfile();
//        user1.setPhone(user.getPhone());
//        user1.setUsername(user.getUsername());
//        user1.setDoctorName(user.getDoctorName());
//        user1.setGender(user.getGender());
//        user1.setDoctorAvatar(user.getDoctorAvatar());
//        user1.setDoctorExpertise(user.getDoctorExpertise());
//        user1.setDoctorIntro(user.getDoctorIntro());
//
//        user1.setDoctorRoleId(user.getDoctorRoleId());

        return i;
    }

    @Override
    public PersonalProfile findUserById(Long doctorId) {
        PersonalProfile personalProfile = personalProfileMapper.selectUserByIdWithRole(doctorId);
        return personalProfile;
    }

    @Override
    public Integer modifyPassword(Long doctorId, String password) {
        String hashpw = BCrypt.hashpw(password);
        LambdaUpdateWrapper<PersonalProfile> wrapper = Wrappers.lambdaUpdate(PersonalProfile.class)
                .eq(PersonalProfile::getId, doctorId)
                .set(PersonalProfile::getPassword, hashpw);
        int i = personalProfileMapper.update(null, wrapper);
        return i;
    }
}




